#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@author:  runyuanye
@contact: runyuanye@163.com
"""
import os

feature_list_files = [
    r'/rootfs/media/yery/Kaso/data/VideoGenderCropPerson/BoxFeature.txt',
    r'/rootfs/media/yery/Kaso/data/ErisedPersonGender/BoxFeature.txt',
]
feature_data_files = [
    r'/rootfs/media/yery/Kaso/data/VideoGenderCropPerson/Feature.dat',
    r'/rootfs/media/yery/Kaso/data/ErisedPersonGender/Feature.dat',
]
out_feature_list_path = r'/rootfs/media/yery/Kaso/data/VideoGenderCropPerson/BoxFeatureMerge.txt'
out_feature_data_path = r'/rootfs/media/yery/Kaso/data/VideoGenderCropPerson/FeatureMerge.dat'


def main():
    file_features = {}
    file_count = 0
    for file_list, feature_file in zip(feature_list_files, feature_data_files):
        feat_file = open(feature_file, 'rb')
        with open(file_list, 'r') as file:
            for line in file.readlines():
                file_count += 1
                if file_count % 1000 == 0:
                    print('Read Feature Count: {}'.format(file_count))
                    # break

                # gd/zs/dqjd/xgly/150100414a5444345203bcd04294b500/video/20190912_154308/00010_000.jpg 154,69,290,245,1,1032
                lines = line.strip().split()
                file_name = lines[0]
                feat_size = int(lines[1].split(',')[-1])
                feature = feat_file.read(feat_size)
                if file_features.get(file_name, None) is None:
                    file_features[file_name] = [line, feature]
        feat_file.close()
    print('Read Feature Count: {}'.format(file_count))

    file_names = list(file_features.keys())
    file_names.sort()
    file_count = 0
    out_feature_list_file = open(out_feature_list_path, 'w')
    out_feature_data_file = open(out_feature_data_path, 'wb')
    for file_name in file_names:
        line, feature = file_features[file_name]
        out_feature_list_file.write(line)
        out_feature_data_file.write(feature)
        file_count += 1
        if file_count % 1000 == 0:
            print('Write Feature Count: {}'.format(file_count))
    print('Write Feature Count: {}'.format(file_count))
    out_feature_list_file.close()
    out_feature_data_file.close()
    os.system('chmod a+wr {}'.format(out_feature_list_path))
    os.system('chmod a+wr {}'.format(out_feature_data_path))


if __name__ == '__main__':
    main()
